package com.hc.serviceImpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.hc.bean.UserBean;
import com.hc.dao.RunSqlDao;
import com.hc.dao.UserDao;
import com.hc.daoImpl.RunSqlDaoImpl;
import com.hc.daoImpl.UserDaoImpl;
import com.hc.service.UserService;
import com.hc.util.DateUtil;

public class UserServiceImpl implements UserService{

	@Override
	public boolean userLogin(String name, String pwd) {
		
		
		SimpleDateFormat format = new SimpleDateFormat(DateUtil.DEFAULT_STRING);
		
		String sql = "select pwd  from users where name = '"+ name+"'";
		
		RunSqlDao dao = new RunSqlDaoImpl();
		List list = dao.runSqlQuery(sql);
		
		for(Object obj:list){
			Map map = (Map) obj;
			if( ((String) map.get("pwd")).equals(pwd)){
				//修改最近登录时间
				String uodateTimeSql = "update users set lastLoginTime = '"+format.format(new Date())+"' WHERE name = '"+name+"'";
				dao.runSqlUpdate(uodateTimeSql);
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean addUser(String name, String pwd,String remark) {
		
		UserDao dao =new  UserDaoImpl();
		UserBean bean = new UserBean();
		bean.setName(name);
		bean.setPwd(pwd);
		bean.setRemark(remark);
		dao.addUser(bean);
		return true;
	}

	@Override
	public boolean updateUserBean(UserBean bean) {
		
		UserDao dao =new  UserDaoImpl();
		dao.updateUserBean(bean);
		return true;
	}

	@Override
	public boolean delUserBean(UserBean bean) {
		
		UserDao dao =new  UserDaoImpl();
		dao.delUserBean(bean);
		return true;
		
	}

}
